package com.start.cms.mappers;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.start.cms.entity.StartCmsSpecialContentEntity;
import com.start.cms.entity.VStartCmsSpecialContentDetailEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface StartCmsSpecialContentMapper extends BaseMapper<StartCmsSpecialContentEntity> {
    @Select("SELECT a.* FROM (\n" +
            "SELECT\n" +
            "`scsc`.`id` AS `id`,\n" +
            "`scsc`.`special_id` AS `special_id`,\n" +
            "`scsc`.`special_content_id` AS `special_content_id`,\n" +
            "`scsc`.`special_content_sort` AS `special_content_sort`,\n" +
            "`scsc`.`create_by` AS `create_by`,\n" +
            "`scsc`.`create_time` AS `create_time`,\n" +
            "`scsc`.`update_by` AS `update_by`,\n" +
            "`scsc`.`update_time` AS `update_time`,\n" +
            "`scs`.`special_title` AS `special_title`,\n" +
            "`scs`.`special_code` AS `special_code`,\n" +
            "`scs`.`special_content_type` AS `special_content_type`,\n" +
            "`scs`.`special_cover` AS `special_cover`,\n" +
            "`scs`.`special_describe` AS `special_describe`,\n" +
            "`scc`.`category_title` AS `special_content_name` \n" +
            "FROM\n" +
            "`start_cms_special_content` `scsc`\n" +
            "INNER JOIN `start_cms_special` `scs` ON `scs`.`id` = `scsc`.`special_id` \n" +
            "AND `scs`.`special_content_type` = 'special:content:type:category'\n" +
            "INNER JOIN `start_cms_category` `scc` ON `scc`.`id` = `scsc`.`special_content_id`\n" +
            "UNION\n" +
            "SELECT\n" +
            "`scsc`.`id` AS `id`,\n" +
            "`scsc`.`special_id` AS `special_id`,\n" +
            "`scsc`.`special_content_id` AS `special_content_id`,\n" +
            "`scsc`.`special_content_sort` AS `special_content_sort`,\n" +
            "`scsc`.`create_by` AS `create_by`,\n" +
            "`scsc`.`create_time` AS `create_time`,\n" +
            "`scsc`.`update_by` AS `update_by`,\n" +
            "`scsc`.`update_time` AS `update_time`,\n" +
            "`scs`.`special_title` AS `special_title`,\n" +
            "`scs`.`special_code` AS `special_code`,\n" +
            "`scs`.`special_content_type` AS `special_content_type`,\n" +
            "`scs`.`special_cover` AS `special_cover`,\n" +
            "`scs`.`special_describe` AS `special_describe`,\n" +
            "`sca`.`article_title` AS `special_content_name` \n" +
            "FROM\n" +
            "`start_cms_special_content` `scsc`\n" +
            "INNER JOIN `start_cms_special` `scs` ON `scs`.`id` = `scsc`.`special_id` \n" +
            "AND `scs`.`special_content_type` = 'special:content:type:article'\n" +
            "INNER JOIN `start_cms_article` `sca` ON `sca`.`id` = `scsc`.`special_content_id`\n" +
            ") a ${ew.customSqlSegment}")
    Page<VStartCmsSpecialContentDetailEntity> pageSpecialContentDetail(@Param("page") Page<VStartCmsSpecialContentDetailEntity> page, @Param(Constants.WRAPPER) Wrapper<VStartCmsSpecialContentDetailEntity> wrapper);

    String SPECIAL_CONTENT_BY_CATEGORY = "SELECT\n" +
            "a.* \n" +
            "FROM\n" +
            "(\n" +
            "SELECT\n" +
            "scsc.id AS id,\n" +
            "scsc.special_id AS special_id,\n" +
            "scsc.special_content_id AS special_content_id,\n" +
            "scsc.special_content_sort AS special_content_sort,\n" +
            "scsc.create_by AS create_by,\n" +
            "scsc.create_time AS create_time,\n" +
            "scsc.update_by AS update_by,\n" +
            "scsc.update_time AS update_time,\n" +
            "scs.special_title AS special_title,\n" +
            "scs.special_code AS special_code,\n" +
            "scs.special_content_type AS special_content_type,\n" +
            "scs.special_cover AS special_cover,\n" +
            "scs.special_describe AS special_describe,\n" +
            "scc.category_title AS special_content_name \n" +
            "FROM\n" +
            "start_cms_special_content scsc\n" +
            "INNER JOIN start_cms_special scs ON scs.id = scsc.special_id \n" +
            "AND scs.special_content_type = 'special:content:type:category'\n" +
            "INNER JOIN start_cms_category scc ON scc.id = scsc.special_content_id \n" +
            ") AS a";

    @Select({SPECIAL_CONTENT_BY_CATEGORY + " ${ew.customSqlSegment}"})
    List<VStartCmsSpecialContentDetailEntity> listSpecialContentDetailByCategory(@Param(Constants.WRAPPER) Wrapper<VStartCmsSpecialContentDetailEntity> wrapper);

    String SPECIAL_CONTENT_BY_ARTICLE = "SELECT\n" +
            "a.* \n" +
            "FROM\n" +
            "(\n" +
            "SELECT\n" +
            "`scsc`.`id` AS `id`,\n" +
            "`scsc`.`special_id` AS `special_id`,\n" +
            "`scsc`.`special_content_id` AS `special_content_id`,\n" +
            "`scsc`.`special_content_sort` AS `special_content_sort`,\n" +
            "`scsc`.`create_by` AS `create_by`,\n" +
            "`scsc`.`create_time` AS `create_time`,\n" +
            "`scsc`.`update_by` AS `update_by`,\n" +
            "`scsc`.`update_time` AS `update_time`,\n" +
            "`scs`.`special_title` AS `special_title`,\n" +
            "`scs`.`special_code` AS `special_code`,\n" +
            "`scs`.`special_content_type` AS `special_content_type`,\n" +
            "`scs`.`special_cover` AS `special_cover`,\n" +
            "`scs`.`special_describe` AS `special_describe`,\n" +
            "`sca`.`article_title` AS `special_content_name` \n" +
            "FROM\n" +
            "`start_cms_special_content` `scsc`\n" +
            "INNER JOIN `start_cms_special` `scs` ON `scs`.`id` = `scsc`.`special_id` \n" +
            "AND `scs`.`special_content_type` = 'special:content:type:article'\n" +
            "INNER JOIN `start_cms_article` `sca` ON `sca`.`id` = `scsc`.`special_content_id` \n" +
            ") a";

    @Select({SPECIAL_CONTENT_BY_ARTICLE + " ${ew.customSqlSegment}"})
    List<VStartCmsSpecialContentDetailEntity> listSpecialContentDetailByArticle(@Param(Constants.WRAPPER) Wrapper<VStartCmsSpecialContentDetailEntity> wrapper);


    @Select("select special_content_id from start_cms_special_content where special_id=#{specialId} order by special_content_sort ,create_time desc ")
    List<String> getListContentIdBySpecialId(@Param("specialId") String specialId);
}
